// pages/myorder/myorder.js
let network = require("../../utils/network.js")

let app = getApp();
let url = app.globalData.url;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab_list: ['全部', '待支付', '待发货', '已发货', '已完成'],
    status: 0,
    alert:true,
    orderList:[],
    gnum:0,//共计num
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      status: options.state
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var arr = []
    network.request({
      url: url +'/AppletsApi/Order/orderList.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
          
        // that.setData({
        //   orderList: res.data.data
        // })
            // 全部
          if (that.data.status == 0) {
              for (var i = 0; i < res.data.data.length; i++) {
                arr.push(res.data.data[i])
              }
              console.log(arr)
              that.setData({
                orderList: arr
              })
            }
            // 待支付
          if (that.data.status == 1) {
              for (var i = 0; i < res.data.data.length; i++) {
                if (res.data.data[i].status == '待支付') {
                  arr.push(res.data.data[i])
                }
              }
              that.setData({
                orderList: arr
              })
            }
            // 已发货
          if (that.data.status == 3) {
              for (var i = 0; i < res.data.data.length; i++) {
                if (res.data.data[i].status == '已发货') {
                  arr.push(res.data.data[i])
                }
              }
              that.setData({
                orderList: arr
              })
            }
            // 已完成
        if (that.data.status == 4) {
              for (var i = 0; i < res.data.data.length; i++) {
                if (res.data.data[i].status == '已完成') {
                  arr.push(res.data.data[i])
                }
              }
              that.setData({
                orderList: arr
              })
            }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  delorder:function(e){
    this.setData({
      alert:false
    })
  },

  list:function(e){
    var current = e.currentTarget.dataset.index
    this.setData({
      status: current
    })
    var arr = []
    var that = this
    network.request({
      url: url + '/AppletsApi/Order/orderList.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
             // 全部
            if (current == 0) {
              for (var i = 0; i < res.data.data.length; i++) {
                arr.push(res.data.data[i])
              }
              console.log(arr)
              that.setData({
                orderList: arr
              })
            }
            // 待支付
            if (current == 1){
              for (var i = 0; i < res.data.data.length;i++){
                if (res.data.data[i].status == '待支付'){
                  arr.push(res.data.data[i])
                }   
              }
              that.setData({
                  orderList: arr
                })
            }
          // 待发货
          if (current == 2) {
            for (var i = 0; i < res.data.data.length; i++) {
              if (res.data.data[i].status == '待发货') {
                arr.push(res.data.data[i])
              }
            }
            that.setData({
              orderList: arr
            })
          }
        // 待发货
        if (current == 3) {
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].status == '待收货') {
              arr.push(res.data.data[i])
            }
          }
          that.setData({
            orderList: arr
          })
        }
        // 已完成
        if (current == 4) {
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].status == '已完成') {
              arr.push(res.data.data[i])
            }
          }
          that.setData({
            orderList: arr
          })
        }
      }
    })

    
  },
  // 不删除订单
  cancel:function(){
    this.setData({
      alert: true
    })
  },
  // 删除订单
  confirm: function () {
    this.setData({
      alert: true
    })
  },

  // 
  ddxq:function(e){
    var path = e.currentTarget.dataset.path
    wx.navigateTo({
      url: path,
    })


  },

  orderdetails:function(e){
    var out_trade_no = e.currentTarget.dataset.out_trade_no
    console.log(out_trade_no)
    network.request({
      url: url + '/AppletsApi/Order/orderWechatPay.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        out_trade_no: out_trade_no,
        paytype:1,
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        // 调用支付接口
        wx.requestPayment({
          timeStamp: data.data.timeStamp,
          nonceStr: data.data.nonceStr,
          package: data.data.package,
          signType: data.data.signType,
          paySign: data.data.paySign,
          success(res) {
            wx.navigateTo({
              url: '/pages/myorder/myorder?state=2',
            })
          },
          fail(res) {
            wx.navigateTo({
              url: '/pages/myorder/myorder?state=1',
            })
          }
        })
      }
    })
  },
  myrefund:function(e){
    var path = e.currentTarget.dataset.path
    wx.navigateTo({
      url: path,
    })
  }
})